import { binMsg } from '@/global/api.js';
import localCache from '@/utils/cache.js'

// *在 LoginPanel 组件使用的登录逻辑
// 登录事件：检查账号密码 -> 检查验证码 -> 开始发起登录请求
export function usePanelLogin(RoleRef, varifyRef, isKeepPassword) {
  if (RoleRef.value.isEmpty) return binMsg('warning', '请输入账号密码！')
  if (varifyRef.value.inputCode==='') return binMsg('warning', '请输入验证码！')
  const validateRes = varifyRef.value.validate()
  if (!validateRes) {
    binMsg('error', '验证码错误！')
    varifyRef.value.draw()
    varifyRef.value.inputCode = ''
    return
  }
  RoleRef.value.login(isKeepPassword.value)
}

// *保存用户账号密码
export function useSetCache(isKeepPassword, accountForm) {
  if (isKeepPassword) {
    localCache.setCache('account', accountForm.account)
    localCache.setCache('password', accountForm.password)
  } else {
    localCache.deleteCache('account')
    localCache.deleteCache('password')
  }
}